package gc3d.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Pierrick
 * We can't use FTP as stream server
 * so we have to pass by this servlet
 */
public class GetVideo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public static String cleanStr(String str2clean) {
		 // security
		return str2clean.replace('\\', 's').replace('/' , 's');
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter out = resp.getWriter();
		String file = req.getParameter("video");
		String UUID = req.getParameter("UUID");
		if (file != null && UUID != null) {
			file  = cleanStr(file);
			UUID = cleanStr(UUID);

			int buff;
			FileInputStream fis = new FileInputStream(
					System.getenv("FTP_HOME") 
					+ File.separatorChar + "GridPovTemp" 
					+ File.separatorChar + UUID 
					+ File.separatorChar + file);

			//resp.setContentType("video/avi");
			resp.setContentType("application/mpeg");
			while ((buff = fis.read()) != -1) {
				out.write(buff);
			}
			fis.close();
		}
		else {
			resp.setContentType("text/html");
			out.print("<h1>?video=xxx.pov.avi&UUID=xxx<h1>");
		}
	}
}
